package com.sikaryofficial.backend.controller.pointrule;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sikaryofficial.backend.annotation.TenantFilter;
import com.sikaryofficial.backend.constant.PointRuleEnum;
import com.sikaryofficial.backend.domain.dto.SysDictDataDTO;
import com.sikaryofficial.backend.domain.dto.req.points.PointsRuleReq;
import com.sikaryofficial.backend.domain.dto.resp.points.PointsRuleDTO;
import com.sikaryofficial.backend.domain.dto.resp.points.UserPointsTaskDTO;
import com.sikaryofficial.backend.manager.PointsRuleManager;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.utils.PageUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.PageResult;
import com.sikaryofficial.common.core.web.page.PageDomain;
import com.sikaryofficial.common.log.annotation.Log;
import com.sikaryofficial.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 积分规则Controller
 *
 * @author qinjinyuan
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/pointsRule/backend")
@Api(tags = "V3.5-积分规则后台管理")
public class PointsRuleBackendController extends BaseController {
    @Autowired
    private PointsRuleManager pointsRuleManager;

    /**
     * 积分规则-启用
     */
    @GetMapping("/{pointsRuleId}/{hasEnable}")
    @ApiOperation("积分规则-启用")
    public R<Boolean> ruleEnable(@ApiParam(value = "规则ID") @PathVariable(value = "pointsRuleId") Long pointsRuleId,
                                 @ApiParam(value = "是否启用：0-启用 ,1-禁用") @PathVariable(value = "hasEnable") Integer hasEnable) {
        return R.ok(pointsRuleManager.ruleEnable(pointsRuleId, hasEnable));
    }

    /**
     * 积分名称及编码下拉列表
     */
    @GetMapping("/ruleNameAndCode")
    @ApiOperation("积分名称及编码下拉列表")
    public R<List<SysDictDataDTO>> ruleNameAndCode() {
        PointRuleEnum[] reportTypeEnums = PointRuleEnum.values();
        List<SysDictDataDTO> result = new ArrayList<>();
        for (PointRuleEnum reportTypeEnum : reportTypeEnums) {
            SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
            result.add(sysDictDataDTO);
            sysDictDataDTO.setDictLabel(reportTypeEnum.getName());
            sysDictDataDTO.setDictValue(reportTypeEnum.getCode());
            sysDictDataDTO.setDictType("ruleNameAndCode");
        }
        return R.ok(result);
    }

    /**
     * 查询积分规则列表
     */
    @PostMapping("/listPage")
    @ApiOperation("积分规则-分页列表")
    public PageResult<PointsRuleDTO> listPage(@Validated @RequestBody PageDomain req) {
        IPage<PointsRuleDTO> page = pointsRuleManager.listPage(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 获取积分规则详细信息
     */
    @GetMapping(value = "/detail/{pointsRuleId}")
    @ApiOperation("积分规则-详细信息")
    public R<PointsRuleDTO> getInfo(@PathVariable("pointsRuleId") Long pointsRuleId) {
        return R.ok(pointsRuleManager.getInfo(pointsRuleId));
    }


    /**
     * 根据积分编码获取积分规则详细信息
     */
    @GetMapping(value = "/detailByCode/{pointsRuleCode}")
    @ApiOperation("积分规则-根据规则编码获取个人积分任务详情")
    public R<UserPointsTaskDTO> getInfo(@PathVariable("pointsRuleCode") String pointsRuleCode) {
        return R.ok(pointsRuleManager.getInfoByCode(pointsRuleCode));
    }

    /**
     * 新增积分规则
     */
    @Log(title = "积分规则保存", businessType = BusinessType.INSERT)
    @PostMapping(value = "/saveOrUpdate")
    @ApiOperation("积分规则-保存")
    public R<Boolean> saveOrUpdate(@Validated @RequestBody PointsRuleReq req) {
        return R.ok(pointsRuleManager.saveOrUpdate(req));
    }

    /**
     * 后台管理-隐藏活动设置
     */
    @Log(title = "积分规则隐藏设置", businessType = BusinessType.UPDATE)
    @GetMapping("/hide/{pointsRuleId}/{hide}")
    @ApiOperation("V3.5-后台管理-隐藏活动设置")
    @TenantFilter
    public R<Boolean> pointsRuleHidden(
            @ApiParam(value = "积分规则ID", required = true) @PathVariable("pointsRuleId") Long pointsRuleId,
            @ApiParam(value = "是否隐藏活动 0-显示 1-隐藏，默认0-显示", required = true) @PathVariable("hide") Integer hide) {
        return R.ok(pointsRuleManager.pointsRuleHidden(pointsRuleId, hide));
    }
}
